package cn.samler.shopapi.Controller;

import cn.samler.shopapi.Model.Sys.Response;
import cn.samler.shopapi.Service.GoodsService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("goods")
public class GoodsController {
    private final GoodsService goodsService;

    GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 获取商品分类
     *
     * @return List<Classify>
     */
    @GetMapping("classify_list")
    public Response getClassify() {
        return Response.success(goodsService.getClassify());
    }

    /**
     * 获取分类下的商品
     *
     * @param cid  分类id
     * @param page 页数
     * @return List<Goods>
     */
    @GetMapping("goods_list")
    public Response getGoodsByClassify(Integer cid, Integer page, Integer offset) {
        page = page == null || page < 0 ? 1 : page;
        return Response.success(goodsService.getGoodsByClassify(cid, page, offset));
    }

    /**
     * 获取单个商品下的详细信息
     *
     * @param id 商品id
     * @return Goods
     */
    @GetMapping("goods_item")
    public Response getGoodsById(int id) {
        return Response.success(goodsService.getGoodsById(id));
    }
}
